<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>json的返回</title>
    <style>
      .container {
        width: 200px;
        height: 200px;
        border: 1px solid red;
      }
    </style>
</head>
<body>
  <div class="container"></div>
  <script>
    var container = document.querySelector('.container');
    window.onkeydown = function () {
      var xhr = new XMLHttpRequest();
      //设置返回的响应体类型
      xhr.responseText = 'json';
      xhr.open('GET','http://localhost:8000/server-json');
      xhr.send();
      xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
          if (xhr.status >= 200 && xhr.status < 300) {
            //第一种转换方法
            // var data = JSON.parse(xhr.response);
            // console.log(data);
            //第二种方法
            container.innerHTML = xhr.response;
            console.log(xhr.response);
          }
        }
      }
    }
  </script>
</body>
</html>